package com.cindata
import grails.converters.JSON
import grails.gorm.transactions.Transactional
import groovy.transform.CompileStatic
import groovy.transform.TypeChecked

@Transactional
@CompileStatic
@TypeChecked
class RiskWarningService
{
    static scope = "singleton"

    WorkflowInstanceService workflowInstanceService

    def initRiskWarningFlow(RiskWarning riskWarning, Workflow workflow)
    {
        if (workflow)
        {
            def workflowInstance = WorkflowInstance.find("from WorkflowInstance where riskWarning.id = ? and workflow.id = ? and status.name = ?", [riskWarning.id, workflow.id, 'Pending'])
            if(!workflowInstance)
            {
                workflowInstance = new WorkflowInstance()
            }
            else
            {
                println "该工作流已初始化过"
                return
            }
            workflowInstance.workflow = workflow
            workflowInstance.riskWarning = riskWarning
            workflowInstance.name = riskWarning?.serialNumber + "-" + workflow?.name
            if (workflowInstance.validate())
            {
                workflowInstance.save flush: true
            }
            else
            {
                println workflowInstance.errors
            }
            workflowInstanceService.initWorkflowInstance(workflowInstance)
        }
        else
        {
            println "未找到工作流，预警 ${riskWarning} 初始化失败"
        }

    }
}
